package com.microcore.jfreechart;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.block.BlockBorder;
import org.jfree.data.time.Second;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;

import java.awt.*;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * 时序图测试类
 *
 * @author FengJie
 * @date 2018/6/27
 */
public class Timing {
    public static void main(String[] args) throws Exception {

        //test1();

        //(Eb/N0)/(中频电平)
        test2();
    }

    //方位角预测值与实测值时序图
    private static void test1() throws Exception {
        // 2：创建时序图
        JFreeChart chart = ChartFactory.createTimeSeriesChart(
                "时序图",
                "时间",
                "单位(°)",
                createDataset(),
                true,
                true,
                false);

        // 3:设置抗锯齿，防止字体显示不清楚
        ChartUtils.setAntiAlias(chart);

        // 4:对时序图进行渲染
        ChartUtils.setTimeSeriesRender(chart.getXYPlot(), false, false);
        //设置中文字体
        ChartUtils.setTimeSeriesFont(chart);
        chart.getLegend().setFrame(new BlockBorder(Color.WHITE));
        chart.setBackgroundPaint(new Color(254, 255, 246));

        ChartUtils.saveAsFile(chart,
                "C:/Users/21099/Desktop/方位角预测值与实测值时序图.png",
                1024, 420);
    }

    //方位角 专用数据
    private static XYDataset createDataset() {
        List<String> x = new ArrayList<>(500);
        //方位预报值
        List<Double> y1 = new ArrayList<>(500);
        //方位角实测
        List<Double> y2 = new ArrayList<>(500);

        TimeSeriesCollection dataset = null;
        try {
            String path = "F:/微核科技/项目/2018-03-07 故障诊断编码设计工作/RR201612290085/M/1.dat";
            FileInputStream inputStream = new FileInputStream(path);
            InputStreamReader reader = new InputStreamReader(inputStream);
            BufferedReader bufferedReader = new BufferedReader(reader);
            //不要首行数据
            bufferedReader.readLine();

            String line;
            String lastString = "";
            while ((line = bufferedReader.readLine()) != null) {
                String[] body = line.split("\t");
                if (lastString.equals(body[0])) {
                    continue;
                } else {
                    x.add(body[0]);
                    lastString = body[0];
                }
                y1.add(Double.parseDouble(body[4]));
                y2.add(Double.parseDouble(body[1]));
            }
            bufferedReader.close();
            reader.close();
            inputStream.close();

            dataset = new TimeSeriesCollection();
            TimeSeries timeSeries1 = new TimeSeries("方位角预测值", Second.class);
            TimeSeries timeSeries2 = new TimeSeries("方位角实测值", Second.class);
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for (int i = 0; i < x.size(); i++) {
                Second second = new Second(format.parse(x.get(i)));
                timeSeries1.add(second, y1.get(i));
                timeSeries2.add(second, y2.get(i));
            }
            dataset.addSeries(timeSeries1);
            dataset.addSeries(timeSeries2);
        } catch (IOException i) {

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dataset;
    }


    //(Eb/N0)/(中频电平)时序图
    private static void test2() throws Exception {
        // 2：创建时序图
        JFreeChart chart = ChartFactory.createTimeSeriesChart(
                "Eb/N0)/(中频电平) 时序图",
                "时间",
                "单位(dBm)",
                createDataset1(),
                true,
                true,
                false);

        // 3:设置抗锯齿，防止字体显示不清楚
        ChartUtils.setAntiAlias(chart);

        // 4:对时序图进行渲染
        ChartUtils.setTimeSeriesRender(chart.getXYPlot(), false, false);
        //设置中文字体
        ChartUtils.setTimeSeriesFont(chart);
        chart.getLegend().setFrame(new BlockBorder(Color.WHITE));
        chart.setBackgroundPaint(new Color(254, 255, 246));

        ChartUtils.saveAsFile(chart,
                "C:/Users/21099/Desktop/(Eb|N0)|(中频电平)时序图.png",
                1024, 420);
    }

    //  (Eb/N0)/(中频电平) 专用数据
    private static XYDataset createDataset1() {
        List<String> x = new ArrayList<>(500);
        //Eb/N0
        List<Double> y1 = new ArrayList<>(500);
        //中频电平
        List<Double> y2 = new ArrayList<>(500);

        TimeSeriesCollection dataset = null;
        try {
            String path = "F:/微核科技/项目/2018-03-07 故障诊断编码设计工作/RR201612290085/M/1.dat";
            FileInputStream inputStream = new FileInputStream(path);
            InputStreamReader reader = new InputStreamReader(inputStream);
            BufferedReader bufferedReader = new BufferedReader(reader);
            //不要首行数据
            bufferedReader.readLine();

            String line;
            String lastString = "";//处理第一列数据相同情况
            while ((line = bufferedReader.readLine()) != null) {
                String[] body = line.split("\t");
                if (lastString.equals(body[0])) {
                    continue;
                } else {
                    x.add(body[0]);
                    lastString = body[0];
                }
                y1.add(Double.parseDouble(body[19]));
                y2.add(Double.parseDouble(body[18]));
            }
            bufferedReader.close();
            reader.close();
            inputStream.close();

            dataset = new TimeSeriesCollection();
            TimeSeries timeSeries1 = new TimeSeries("Eb/N0", Second.class);
            TimeSeries timeSeries2 = new TimeSeries("中频电平", Second.class);
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for (int i = 0; i < x.size(); i++) {
                Second second = new Second(format.parse(x.get(i)));
                timeSeries1.add(second, y1.get(i));
                timeSeries2.add(second, y2.get(i));
            }
            dataset.addSeries(timeSeries1);
            dataset.addSeries(timeSeries2);
        } catch (IOException i) {

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dataset;
    }

}
